package com.unbank.shuju.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.unbank.shuju.dao.LujiazuiDaoImpl;
import com.unbank.util.JsoupUtil;
import com.unbank.util.StringUtil;

public class LujiazuiAction {
	// 陆家嘴早餐 static int website_id = 2253;
	static String url = "http://114.80.154.46/briefingdeploy/%7Bf1b2dffb-3c26-4f59-969d-5a8e2fd7dcaf%7D.xml";
	// 每日理财观察 2255
	static String url2 = "http://114.80.154.46/briefingdeploy/%7B4dc18465-b34f-46ef-80ef-0b7f1cafecb3%7D.xml";

	// 股市投资参考：
	static String url3 = "http://114.80.154.46/briefingdeploy/%7B308854bf-d554-494f-82ce-9f56eb680584%7D.xml";

	// 期市投资参考
	static String url4 = "http://114.80.154.46/briefingdeploy/%7B618fe2e5-1a7e-4097-8299-5eb5bde3b110%7D.xml";

	// 基金投资参考
	static String url5 = "http://114.80.154.46/briefingdeploy/%7Bbfee925d-50bb-4e5d-b671-332d337cf7b1%7D.xml";

	// 信托行业日报
	static String url6 = "http://114.80.154.46/briefingdeploy/%7Bb0dcb65a-36e4-4ab7-acb3-dc5ab8fab533%7D.xml";

	// 固定收益日报
	static String url7 = "http://114.80.154.46/briefingdeploy/%7Ba0daf6bb-5113-42d1-b557-b2f924000d90%7D.xml";

	// 每日香江速递
	static String url8 = "http://114.80.154.46/briefingdeploy/%7B27c38eac-d26a-41ec-bc7b-8ba44751079a%7D.xml";

	// 金融街下午茶
//	static String url9 = "http://114.80.154.46/briefingdeploy/%7B3e92bcbc-3863-4d31-b316-043abf54c48a%7D.xml";

	public static void main(String[] args) {
		List<String> urls = new ArrayList<String>();
		urls.add(url);
		urls.add(url2);
		urls.add(url3);
		urls.add(url4);
		urls.add(url5);
		urls.add(url6);
		urls.add(url7);
		urls.add(url8);
//		urls.add(url9);

		for (String string : urls) {
			Document document = JsoupUtil.readUrl(string);
			Elements dates = document.select("newsdate");
			// 日期
			String time = dates.get(0).text();

			Elements columnElements = document.select("column");
			for (Element columnElement : columnElements) {
				String column = columnElement.attr("name");
				Elements columns = columnElement.select("news");
				for (Element element : columns) {
					String title = element.attr("title").replace("'", "");
					String webname = "陆家嘴早餐";
					String content = element.text();
					content = content.replaceAll(" style=\'\\S[^\']*\'", " ");
					content = content.replace("'", "");
					StringBuffer sb = new StringBuffer();
					sb.append("insert into  ptf_crawl (website_id,crawl_title,web_name,url,news_time,crawl_time,crawl_brief,task)  values ( 2253,'");
					// insert into ptf_crawl
					// (website_id,crawl_title,web_name,url,news_time,crawl_time)
					// values (?,?,?,?,?, NOW())
					// insert into ptf_crawl_text (crawl_id ,text) values (?,?)
					// 标题
					// System.out.println(title);
					String url = new Date().getTime() + "" + string;
					sb.append("【" + column + "】" + title + "','");
					sb.append(webname + "','");
					sb.append(url + "','");
					time = StringUtil.dateToString(StringUtil
							.stringToDate(time));
					String bref = "";
					if (content.length() < 100) {
						bref = content;
					} else {
						bref = content.substring(0, 100);
					}
					sb.append(time + "', NOW() , '" + bref + "',1) ");
					// 内容
					// System.out.println(content);

					// webSite ID

					// WebName

					// URL
					System.out.println(sb.toString());
					// 保存到数据库
					int id = LujiazuiDaoImpl.insertTitle(sb.toString());
					System.out.println(id);
					String contetn_sql = "insert into ptf_crawl_text (crawl_id ,text) values ("
							+ id + ",'" + content + "')";
					System.out.println(contetn_sql);
					LujiazuiDaoImpl.insertContent(contetn_sql);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					// 保存到数据库结束

				}

			}

		}

	}

}
